import {
    ADD_TABS,
    REMOVE_TABS,
    CHANGE_TABS,
    FILTER_TABS,
    SET_USERINFO,
} from './actionTypes'
import { createBrowserHistory } from 'history'
// import { themeConfig } from '@/config'
import { storage } from '@/utils/storage'
import {
    addTabs,
    removeTabs,
    changeTabs,
    filterTabs,
    setUserInfo,
    logout
} from './actionCreators'
const history = createBrowserHistory();
let tabList = storage.get('tabList')
let userInfo = storage.get('userInfo')
let permissionCodes = userInfo && userInfo !== null ? userInfo.permissionCodes : []
const defaultState = {
    keyPath: '/',
    defaultSelectedKeys: [],
    defaultOpenKeys: ['/userCenter', '/baseSetting'],
    selectedKeys: '',
    openKeys: ['/userCenter', '/baseSetting'],
    activeKey: history.location.pathname || '/',
    panes: tabList && JSON.stringify(tabList) !== '{}' ? tabList : [
        { title: '首页', key: '/', meta: { roles: ['user_index'] }, closable: false }
    ],
    permissionCodes: permissionCodes || [],
    history,
    userInfo: userInfo
}
const appState = (state = defaultState, action) => {
    switch (action.type) {
        case ADD_TABS:
            return {
                ...state,
                ...addTabs(state, action)
            }
        case REMOVE_TABS:
            return {
                ...state,
                ...removeTabs(state, action)
            }
        case CHANGE_TABS:
            return {
                ...state,
                ...changeTabs(state, action)
            }
        case FILTER_TABS:
            return {
                ...state,
                ...filterTabs(state, action)
            }
        case SET_USERINFO:
            return {
                ...state,
                ...setUserInfo(state, action)
            }
        case 'logout':
            return {
                ...state,
                ...logout(state, action)
            }
        default:
            return state
    }
}
export {
    appState
}